package com.azt.provider.impl;

import com.azt.api.dto.IndexBrand;
import com.azt.api.pojo.RecommendBrand;
import com.azt.api.service.RecommendBrandService;
import com.azt.model.page.Pagination;
import com.azt.provider.mapper.RecommendBrandMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xiaoleilu.hutool.convert.Convert;
import com.xiaoleilu.hutool.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
@Transactional
public class RecommendBrandServiceImpl  implements RecommendBrandService {
	
	@Autowired
	private RecommendBrandMapper mapper;

	@Override
	public List<IndexBrand> findIndexBrand() {
        return mapper.findIndexBrand();
	}

	@Override
	public Pagination getRecommendBrandPage(int pageno, int pagesize, Map<String, String> searchMap) {
		Page<Object> startPage = PageHelper.startPage(pageno, pagesize);
		List<RecommendBrand> recommendBrandList = mapper.searchRecommendBrand(searchMap);
		Pagination p = new Pagination(startPage, recommendBrandList);
		p.setList(recommendBrandList);
		p.setParams(searchMap);
		return p;
	}

	@Override
	public RecommendBrand getRecommendBrandById(Integer id) {
		return mapper.selectByPrimaryKey(id);
	}

	@Override
	public void saveOrUpdateRecommendBrand(RecommendBrand recommendBrand) {
		if(recommendBrand.getId() == null) mapper.insert(recommendBrand);
		else mapper.updateByPrimaryKey(recommendBrand);
	}

	@Override
	public void deleteRecommendBrand(RecommendBrand recommendBrand) {
		if(recommendBrand != null) {
			mapper.delete(recommendBrand);
		}
	}

	@Override
	public RecommendBrand queryById(Integer id) {
		return mapper.selectByPrimaryKey(id);
	}

	@Override
	public List<RecommendBrand> searchBrand(Map param) {
		String categorySId = Convert.toStr(param.get("categorySId"));
		if(StrUtil.isNotBlank(categorySId)) {
			String[] array = categorySId.split(",");
			param.put("categorySId", array);
		}
		return mapper.searchRecommendBrand(param);
	}
	
	@Override
	public int getTotalCount() {
		return mapper.selectCount(new RecommendBrand());
	}

	
}

